<template>
	<div style="background-color: #fff;width: 1000px;padding: 20px;">
		<div style="display: flex;margin-bottom: 20px;">
			
		    <div style="margin-right:20px;">
		        
				<el-button @click="addForm" size="mini" type="primary">新增</el-button>
				<el-button @click="delRow" :disabled="status==1"  size="mini" type="primary">删除</el-button>
				<el-button  @click="save" 
					:disabled="status==1"
					:loading="loading_btn"
					size="mini" type="primary">保存</el-button>
				<el-button  size="mini" @click="prevNextPage(1)" type="primary">上一条</el-button>
				<el-button   size="mini" @click="prevNextPage(2)" type="primary">下一条</el-button>
				
		    </div>
			<div style="margin-top: 2px;">
				<el-checkbox v-model="orderChecked" border size="mini">保存后新增</el-checkbox>
			</div>
		</div>
		<el-form ref="form" :model="form" label-width="80px">
			<el-form-item label="单据号">
			    <el-input size="mini" disabled v-model="order"></el-input>
			</el-form-item>
			<el-form-item :label="ttype==1?'付款日期':'收款日期'">
				<el-date-picker
				      size="mini"
				      v-model="form.buyer_date"
					  value-format="yyyy-MM-dd"
				      type="date"
				      placeholder="选择日期时间">
				</el-date-picker>
			</el-form-item>
			<el-form-item label="制单人">
			    <el-input size="mini" disabled v-model="form.buyer_person"></el-input>
			</el-form-item>
			<el-form-item :label="ttype==1?'供应商':'客户'">
			   <selectCustomer :supp="ttype==1?form.supplier:form.customer" 
						:holder="ttype==1?'请选择供应商':'请选择客户'"
						@getCustomerName="getCustomerName" />
			</el-form-item>
			<el-form-item :label="ttype==1?'付款银行':'收款银行'">
				 <el-select size="mini" v-model="form.bank_account" placeholder="请选择">
				    <el-option
				      v-for="item in backList"
				      :key="item.id"
				      :label="item.account_title"
				      :value="item.account_title">
				    </el-option>
				  </el-select>
			</el-form-item>
			<el-form-item :label="ttype==1?'应付金额':'应收金额'">
				<el-input size="mini" disabled v-model="form.pay_put_money "></el-input>
			</el-form-item>
			<el-form-item :label="ttype==1?'付款金额':'收款金额'">
				<el-input size="mini" v-model="form.price"></el-input>
			</el-form-item>
			<!-- <el-form-item label="经手人">
				<el-select size="mini" v-model="form.hander_person" placeholder="请选择">
				   <el-option
				     v-for="item in userList"
				     :key="item.id"
				     :label="item.nick_name"
				     :value="item.nick_name">
				   </el-option>
				 </el-select>
			</el-form-item> -->
			<el-form-item label="摘要">
				<el-input type="textarea" v-model="form.remark"></el-input>
			</el-form-item>
		</el-form>	
	</div>
</template>

<script>
	import selectCustomer from "@/components/selectCustomer"
	export default {
		components:{
			selectCustomer
		},
		props:{
			ttype:{
				type:String,
				default:'0'
			}
		},
		data(){
			return {
				form:{
					id:undefined,
					buyer_date:'',
					buyer_person:"",
					supplier:"",
					bank_account:"",
					price:'',
					pay_put_money:'',
					hander_person:"",
					remark:'',
					residue_money:"",
					order_number:"",
					customer:'',
				},
				userList:[],
				backList:[],
				order_number:"",
				pageSize:10,
				page:1,
				status:0
			}
		},
		mounted() {
			this.form.buyer_date = this.$utils.formatNowDate(2)
			this.form.buyer_person = sessionStorage.getItem('user_name')
			this.lodeUserList()
			this.lodeBackList()
			this.init()
			
		},
		methods:{
			init(){
			   let order_number = this.$route.query.order_number
				if(order_number){
					this.order_number = order_number
					this.lodeList()
				}
			},
			prevNextPage(tt){
				// tt 1 上一条 2下一条
				let params = {
					type:this.ttype,
					tt:tt,
					order_number:this.$route.query.order_number || -1,
				}
				this.$post('/putpay/prevNextBtn',params,res=>{
					let result = res.data
					if(result.code==200){
						let order_number = result.data.order_number_new
						if(order_number){
							this.order_number = order_number
							this.$router.replace({
								path:this.$route.path,
								query:{
									order_number:order_number
								}
							})
							this.init()
						}
					}
				})
			},
			lodeList(){
				let params = {
					search_form:this.search_form,
					page:this.page,
					pageSize:this.pageSize,
					type:this.ttype,
					status:0,
					order_number:this.order_number,
				}
				this.$post('/putpay/getPayInfoList',params,res=>{
					let data = res.data.data
					if(this.order_number){
						let item = data.result[0]
						for(let i in this.form){
							this.form[i] = item[i]
						}
						this.status = item.status
						if(this.ttype==2){
							this.form.buyer_date = item.buyer_date_wholesaler
						}
						
						if(this.ttype == 1){
							this.getAmount(this.form.supplier)
						}else{
							this.getAmount(this.form.customer)
						}
						
					}
				})
			},
			delRow(){
				if(!this.form.id){
					this.addForm()
					return
				} 
				this.$confirm('确定删除', '提示', {
						  confirmButtonText: '确定',
						  cancelButtonText: '取消',
						  type: 'warning'
						}).then(() => {
						   this.$post('/putpay/delPayInfo',{id:this.form.id},res=>{
							   if(res.data.code == 200){
								   this.$message.success('删除成功')
								   this.addForm()
							   }
						   })
						})
			},
			save(){
				
				this.form.type = this.ttype
				this.form.residue_money =   this.form.price
				if(this.ttype==2){
					this.form.buyer_date_wholesaler = this.form.buyer_date
				}
				if(!this.form.order_number){
					this.form.order_number = this.$utils.generateRandomString(2)
				}
				
				if(this.ttype==1){
					if(!this.form.supplier) return this.$message.error('请选择供应商')
					if(!this.form.bank_account) return this.$message.error('请选择付款银行')
				}else{
					if(!this.form.customer) return this.$message.error('请选择客户')
					if(!this.form.bank_account) return this.$message.error('请选择收款银行')
				}
				
				this.loading_btn = true
				this.$post('/putpay/addEditPayInfo',this.form,res=>{
					if(this.orderChecked){
						this.addForm()
					}
					this.$message.success('操作成功')
					this.loading_btn = false
				})
			},
			addForm(){
				this.form={
					id:undefined,
					buyer_date:'',
					buyer_person:"",
					supplier:"",
					bank_account:"",
					price:'',
					pay_put_money:'',
					hander_person:"",
					remark:'',
					residue_money:"",
					customer:'',
					order_number:''
				}
				this.order_number = ''
				this.form.buyer_date = this.$utils.formatNowDate(2)
				this.form.buyer_person = sessionStorage.getItem('user_name')
				this.$router.replace({
					path:this.$route.path,
					query:{
						
					}
				})
			},
			getCustomerName(val){
				if(this.ttype == 1){
					this.form.supplier = val
					this.getAmount(val)
				}else{
					this.form.customer = val
					this.getAmount(val)
				}
			},
			//获取应付金额
			getAmount(customer){
				if(!customer) return
				let params = {
					customer:customer,
					start_time:'',
					end_time:'',
					type:this.ttype,
					checked:false
				}
				this.$post('/putpay/getPayPutData',params,res=>{
					this.form.pay_put_money = 0
					
					let result = res.data
					if(result.code==200){
						let item = result.data
						if(item.length){
							let list = item[0].list
							if(list.length){
								for(let el of list){
									if(!el.goods_number) el.goods_number = 1
									if(el.code_arr || el.code){
										if(el.code){
											if(this.ttype==2){
												el.price = -el.sales_price
											}else{
												el.price = -el.price
											}
										}else{
											if(this.ttype==2){
												el.price = el.sales_price
											}else{
												el.price = el.price
											}
										}
										// el.residue_money = el.residue_money?el.residue_money:el.price
										// el.residue_money_put = el.residue_money_put?el.residue_money_put:el.price
										el.residue_money = el.price*el.goods_number
										el.residue_money_put = el.price*el.goods_number
										
									}else{
										el.price = -el.price
										el.residue_money = el.price*el.goods_number
										if(el.residue_money>0) el.residue_money = -el.residue_money
									}
								}
								
								let sum = 0
								for(let item of list){
									// if(this.ttype==1){
									// 	// if(item.residue_money>0) item.residue_money = -item.residue_money
									// 	sum+=item.residue_money*1
									// }else{
									// 	if(item.residue_money>0) item.residue_money = -item.residue_money
									// 	let money_put = item.code || item.code_arr?item.residue_money_put:item.residue_money
									// 	sum+=money_put*1
									// }
									
									sum+=item.price*Math.abs(item.goods_number) 
									
								}
								this.form.pay_put_money = sum
								
							}
						}
						
					}
				})
			},
			lodeUserList(){
				this.$post('/system/userList',{},res=>{
					this.userList = res.data.data
				})
			},
			lodeBackList(){
				this.$post('/baseInfo/getAccountList',{},res=>{
					this.backList = res.data.data
				})
			}
		}
	}
</script>

<style>
</style>